package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.messaging.simp.config.SimpleBrokerRegistration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

import javax.annotation.Resource;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
//    @Resource
//    private TaskScheduler messageBrokerTaskScheduler;
    @Bean
    public TaskScheduler taskScheduler() {
        System.out.println("Creating TaskScheduler bean");
        return new ConcurrentTaskScheduler(); // 或使用其他实现
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
         config.enableSimpleBroker("/topic") // 使用简单的内存消息代理
               .setHeartbeatValue(new long[]{50000, 50000})
                 .setTaskScheduler(taskScheduler()); // 配置心跳间隔30s
//              .setTaskScheduler(taskScheduler()); // 设置 TaskScheduler
        config.setApplicationDestinationPrefixes("/app"); // 设置应用前缀
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws").withSockJS(); // 注册 WebSocket 端点
    }

}

